import { bedList } from '@/services/deptMsg';
import { deviceList } from '@/services/deviceMsg';
import { patientAdd, patientUpdate } from '@/services/patientMsg';
import {
  ModalForm,
  ProFormDigit,
  ProFormSelect,
  ProFormText,
} from '@ant-design/pro-components';
import { Button, Col, Form, Row, message } from 'antd';
import React, { useImperativeHandle, useState } from 'react';

const ItemOption = React.forwardRef((props: any, ref) => {
  const { refreshList } = props;
  const [form] = Form.useForm<any>();
  const [modalOpen, setModalOpen] = useState(false);
  const [modItem, setModItem] = useState<any>(null);
  const handleModalShow = async (row?: any) => {
    setModItem(row || null);
    if(!modalOpen){
      form.resetFields()  
    }
    if (row) {
      form.setFieldsValue({
        ...row,
        sex: row?.sex ? String(row.sex) : undefined,
        blood: row?.blood ? String(row.blood) : undefined,
      });
    }
    setModalOpen(!modalOpen);
  };

  useImperativeHandle(ref, () => ({
    handleModalShow,
  }));
  return (
    <ModalForm
      title={modItem?.id ? '修改患者' : '新增患者'}
      width={600}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnHidden: true,
        onCancel: handleModalShow,
        centered: true,
        maskClosable: false,
      }}
      open={modalOpen}
      submitter={{
        render: (props) => {
          let optBtns: any = [
            <Button
              className="base-button"
              key="cancel"
              onClick={handleModalShow}
            >
              取消
            </Button>,
            <Button
              className="base-button"
              type="primary"
              key="submit"
              onClick={() => props.form?.submit?.()}
            >
              确定
            </Button>,
          ];
          return optBtns;
        },
      }}
      onFinish={async (values) => {
        const sendData = {
          ...values,
          sex: values.sex ? Number(values.sex) : '',
          blood: values.blood ? Number(values.blood) : '',
          // deviceIds: values?.deviceIds?.join(','),
        };
        let res = null;
        if (modItem?.id) {
          //编辑
          res = await patientUpdate({
            ...sendData,
            id: modItem?.id,
          });
        } else {
          //添加
          res = await patientAdd(sendData);
        }
        if (res?.code === 200) {
          message.success('操作成功');
          refreshList();
          setModalOpen(false);
        }
        return true;
      }}
    >
      <Row gutter={20}>
        <Col span={12}>
          <ProFormText
            name="name"
            label="患者姓名"
            fieldProps={{
              maxLength: 100,
              showCount: true,
            }}
          />
        </Col>
        <Col span={12}>
          <ProFormSelect
            name="sex"
            label="患者性别"
            placeholder="请选择患者性别"
            options={[
              {
                label: '男',
                value: '1',
              },
              {
                label: '女',
                value: '2',
              },
            ]}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={7}>
          <ProFormSelect
            name="blood"
            label="患者血型"
            placeholder="请选择患者血型"
            options={[
              {
                label: 'A型',
                value: '1',
              },
              {
                label: 'B型',
                value: '2',
              },
              {
                label: 'AB型',
                value: '3',
              },
              {
                label: 'O型',
                value: '4',
              },
              {
                label: 'RH型',
                value: '5',
              },
            ]}
          />
        </Col>
        <Col span={10}>
          <ProFormText
            name="phone"
            label="患者手机号"
            fieldProps={{
              maxLength: 11,
              showCount: true,
            }}
            placeholder="请输入患者手机号"
          />
        </Col>
        <Col span={7}>
          <ProFormDigit
            name="age"
            label="患者年龄"
            min={1}
            max={100}
            fieldProps={{ precision: 0 }}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={24}>
          <ProFormSelect
            name="bedId"
            label="关联病床"
            placeholder="请选择关联病床"
            allowClear
            request={async () => {
              const res = await bedList({
                pageSize: 1000,
                pageNum: 1,
              });
              if (res?.code === 200) {
                return res.rows.map((its: any) => {
                  return {
                    label: its.code,
                    value: its.id,
                  };
                });
              } else {
                return [];
              }
            }}
          />
        </Col>
      </Row>
      {/* <Row gutter={20}>
        <Col span={24}>
          <ProFormSelect
            name="deviceIds"
            label="关联设备"
            placeholder="请选择关联设备"
            request={async () => {
              const res = await deviceList({
                pageNum: 1,
                pageSize: 10000,
              });
              if (res?.code === 200) {
                return res.rows;
              } else {
                return [];
              }
            }}
            fieldProps={{
              mode: 'multiple',
              fieldNames: {
                label: 'name',
                value: 'id',
              },
            }}
          />
        </Col>
      </Row> */}
    </ModalForm>
  );
});

export default ItemOption;
